package builder.trainer.counter;

import java.util.ArrayList;

import builder.trainer.data.DataManager;

public class Counter {

	private Attribute[] attributes;
	private DataManager dataManager;
	private ArrayList<Integer> indexes;
	
	public Counter(DataManager dm) {
		this.dataManager = dm;
	}
	public void countAll(){
		ArrayList<Integer> indexes = new ArrayList<Integer>();
		for (int i = 0; i < this.dataManager.getRecords().size(); i++) {
			indexes.add(i);
		}
		this.count(indexes);
	}
	
	public void count(ArrayList<Integer> indexes) {
		this.indexes = indexes;
		this.initAttributes(this.dataManager.getAttrNames());
		for (Integer index : indexes) {
			String[] record = this.dataManager.getRecords().get(index);
			for (int i = 0; i < record.length; i++) {
				String sValue = record[i];
				Attribute attribute = this.attributes[i];
				attribute.addValue(sValue, index);
			}
		}
	}

	private void initAttributes(String[] attrs) {
		this.attributes = new Attribute[attrs.length];
		for (int i = 0; i < this.attributes.length; i++) {
			this.attributes[i] = new Attribute(attrs[i], this,i);
		}
	}

	public Attribute[] getAttributes() {
		return attributes;
	}

	public int getRecordsNum(){
		return this.indexes.size();
	}
	public void setIndexes(ArrayList<Integer> indexes) {
		this.indexes = indexes;
	}

	public ArrayList<Integer> getIndexes() {
		return indexes;
	}

	public String[] getRecord(int index) {
		return this.dataManager.getRecords().get(index);
	}
}
